<?php

/**
 * Plugin Name:       API接口功能开发示例
 * Plugin URI:        https://www.npc.ink
 * Description:       使用ThinkAPI 服务演示
 * Author:            Npcink
 * AuthorURI:         https://www.npc.ink
 * Requires at least: 6.1
 * Requires PHP:      7.0
 * Version:           0.1.0
 * Author:            The WordPress Contributors
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       accordion
 *
 * @package           create-block
 */


 define('API_DEV_DEMO', 'api_dev_demo');
 define('API_DEV_OPTION', 'api_object_option');

//添加菜单
function api_menu()
{
    add_submenu_page(
        'options-general.php',//设置选项下的子菜单
        'API示例',//页面名称
        'API示例',//菜单名称
        'administrator',//权限
        'api_dev_demo_config',//设置名
        'api_dev_demo_displays',//菜单展示函数
        '90.1', //顺序
    );
}
add_action('admin_menu', 'api_menu');

//添加设置页面
function api_dev_demo_displays()
{

?>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div class="wrap">
    <h1>API 开发示例</h1>
        <h2>原生JS</h2>
        <div id="api"></div>
        <h2>VUE</h2>
        <div id="apis"></div>
    </div>
<?

}

//加载JS
function load_admin_script_api($hook)
{
    //仅在此页面中加载JS
    if ( 'settings_page_api_dev_demo_config' != $hook ) {
        return;
    }

    //获取JS文件路径
    $index_js = plugin_dir_url(__FILE__) . 'js/api.js';
   

    //底部加载JS文件
    wp_enqueue_script(API_DEV_DEMO, $index_js, array(), '1.3', true);
   
   

     // 获取 设置选项 的值
     $default_value = get_option(API_DEV_OPTION);

    //设置数据传给JS
    wp_localize_script(API_DEV_DEMO, 'api_dev_demo_obj', $default_value);
}
add_action('admin_enqueue_scripts', 'load_admin_script_api');



//加载接口
require_once plugin_dir_path(__FILE__) . 'interface.php';

//加载打包文件
require_once plugin_dir_path(__FILE__) . 'vite.php';


//添加设置按钮
add_filter('plugin_action_links_' . plugin_basename(__FILE__), function ($links) {
    $page_url = menu_page_url('api_dev_demo_config', false);
    $links[] = '<a href="' . $page_url . '">' . __('设置', 'n') . '</a>';
    return $links;
});
